//
//  BaseMessage.m
//  integration_test
//
//  Created by mateng on 2024/2/19.
//

#import "BaseMessage.h"
#import "Const.h"
#import "LogUtils.h"
#import "Tools.h"

static const NSInteger TYPE_ASSERT = 7;
static const NSInteger TYPE_DEBUG = 3;
static const NSInteger TYPE_ERROR = 6;
static const NSInteger TYPE_INFO = 4;
static const NSInteger TYPE_VERBOSE = 2;
static const NSInteger TYPE_WARN = 5;

@implementation BaseMessage


- (nonnull id)initWithHandler:(nonnull UTEPedEventHandler *)handler {
    if (self = [super init]) {
        self.eventHander = handler;
        [self notificationListen];
    }
    
    return self;
}

-(void)notificationListen {
}

- (BOOL) methodCallConsumed:(NSString*)method params:(NSDictionary*)params result:(FlutterResult)result
{
    BOOL consumed = true;
    if([method isEqualToString:M_checkPermissions]) {
        result(@YES);
    }
    else if([method isEqualToString:M_queryPermissions]) {
        result(@YES);
    }
    else if([method isEqualToString:M_hasPermission]) {
        result(@YES);
    }
    else if([method isEqualToString:M_toPermissionPage]) {
        [Tools openUrl: UIApplicationOpenSettingsURLString];
        result(@YES);
    }
    else if([method isEqualToString:M_log]) {
        NSInteger logType = [[params objectForKey:KEY_TYPE] intValue];
        NSString* tag = [params objectForKey:KEY_TAG];
        NSString* message= [params objectForKey:KEY_VALUE];
        if (logType > TYPE_VERBOSE && message != nil && message.length > 0) {
            [LogUtils log:tag message:message];
        }
        
        result(@YES);
    }
    else if([method isEqualToString:M_isBluetoothEnable]) {
        BOOL enable = [[UTEBluetoothMgr sharedInstance] isOpenBluetooth];
        result(enable ? @YES:@NO);
    }
    else if([method isEqualToString:M_isConnected] || [method isEqualToString:M_isConnectedGatt]) {
        BOOL connected = NO;
        if ([UTEBluetoothMgr sharedInstance].connnectModel != nil) {
            connected = [UTEBluetoothMgr sharedInstance].connnectModel.isConnected;
        }
        result(connected ? @YES : @NO);
    }
    else if([method isEqualToString:M_isDeviceBusy]) {
        result(@NO);
    }
    else if([method isEqualToString:M_setTimeDisplay]) {
        NSNumber *timeFormat = [params objectForKey:TIME_FORMAT];
        [[UTEDeviceMgr sharedInstance] setTimeDisplay:1 timeType:[timeFormat integerValue] block:^(NSInteger errorCode, NSDictionary *uteDict) {
            result(errorCode == UTEDeviceErrorNil ? @(YES):@(NO));
        }];
    }
    else if([method isEqualToString:M_checkGPS]) {
        result(@YES);
    }
    else if([method isEqualToString:M_launchApp]) {
        NSString* url = [params objectForKey:KEY_VALUE];
        [Tools openUrl:url];
        result(@YES);
    }
    else {
        consumed = false;
    }
    
    return consumed;
}

- (NSString *)getPath
{
    return @"";
}

- (void)syncTimeClock: (void(^)(NSInteger errorCode ,NSDictionary *uteDict))block {
    NSInteger time = [[NSDate date] timeIntervalSince1970];
    NSTimeZone *timeZoneLocal = [NSTimeZone defaultTimeZone];
    NSInteger sec = [timeZoneLocal secondsFromGMT];
    [[UTEDeviceMgr sharedInstance] setTimeClock:time timeZone:sec / 3600 minuteOffset:sec % 60 block:block];
}

- (void)log:(NSString*)message {
    [LogUtils log:[self getPath] message:message];
}

- (BOOL)isSuccess:(NSInteger)errorCode {
    return errorCode == UTEDeviceErrorNil;
}

- (NSInteger) getTimeDisplay {
    NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
    NSRange containsA = [formatStringForHours rangeOfString:@"a"];
    BOOL has_AMPM = containsA.location != NSNotFound;
    
    return has_AMPM ? 1 : 2;
}

- (BOOL)isConnected {
    BOOL connected = NO;
    if ([UTEBluetoothMgr sharedInstance].connnectModel != nil) {
        connected = [UTEBluetoothMgr sharedInstance].connnectModel.isConnected;
    }
    
    return connected;
}
@end
